<?php

namespace App\Models;

class Product extends Base
{
    protected $table = 'mall_products';

    protected $guarded = ["tag_ids", "amount_yuan", "vip_amount_yuan"];

    protected $appends = [ "amount_yuan", "vip_amount_yuan"];
    protected $casts = [
        'image_url' => 'array',
        'video_url' => 'array',
        'specs_detail' => 'array',
        'specs_detail_info' => 'array',
    ];

    public function tags() {
        return $this->belongsToMany(Tag::class, 'mall_product_tags', 'product_id', 'tag_id');
    }

    public function full_reduction() {
        return $this->belongsTo(MallFullReduction::class, 'producer_id', 'merchant_id');
    }



    public function setImageUrlAttribute($val) {
        $this->attributes['image_url'] = $val;
    }

    public function setVideoUrlAttribute($val) {
        $this->attributes['video_url'] = $val;
    }

    public function setSpecsDetailAttribute($val) {
        $this->attributes['specs_detail'] = $val;
    }

    public function setSpecsDetailInfoAttribute($val) {
        $this->attributes['specs_detail_info'] = $val;
    }

    public function getAmountYuanAttribute() {
        return format_yuan($this->amount);
    }

    public function getVipAmountYuanAttribute() {
        return format_yuan($this->vip_amount);
    }
    public function setFareAttribute($fare) {
        $this->attributes['fare'] = $fare * 100;
    }

    public function getFareAttribute($fare) {
        return format_yuan($fare);
    }

    public function setRateAttribute($val) {
        $this->attributes['rate'] = $val * 100;
    }

    public function getRateAttribute($val) {
        return format_yuan($val);
    }


    public function category() {
        return $this->belongsTo(Category::class, 'category_id')->withTrashed();
    }


    public function parent_category() {
        return $this->belongsTo(Category::class, 'parent_category_id');
    }

    public function producer() {
        return $this->belongsTo(Merchant::class, 'producer_id');
    }

}
